package com.atguigu.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {
    //订单超时时间
    private final String timeout = "30m";

    //在支付宝创建的应用的id
    private   String app_id = "2021000121660294";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCAE/3cLhZE+SvG/lxohpAApcjus9NzybuhHAtl3ha4EpyI0QL4E/Wn2R+GrMc98LhMyEkkIkk6ZsTC3OQE1PoDJNfB6IvndhFLjyDJmKSgvzIe7LndknieJ6B9tcY2R5eCsxTeTyJ7eCkKo/x+nPO8iajxRukB0UDeQ11laTtmeZx2TKota+U9WDgm9ndXZENARe+IYytBDnJySoSL+rWA+iVjE3MKi+JaCptOXn8Io2oLtRzoc9//1/PyhuQAIDLlHbLphO3MDZNdkEK6Hy81ImwMYwbKz90R/5ZV5NUeOeT7ukcA6bKoPuv+9e9p0pY47Q+QXt7mgXtno+l8RW5VAgMBAAECggEAVJJe3+QCynqqIJVXWaH5NqRCIPTXXK7J2aGWGSbkY5nOiT1Ss5MidDoX6iruyYTJfdIHEfgd2JWBuZZ4boRfrnHGkoFG2IkK9JTHNrc3hfmaU/dG5/tO8EgEXt4Hy1HMxv4PephQka0PlLoiAU0MWOOhjeif9ZZCOkHLSyqIkaopQlkQnecyNo0OSW5qRIV1X0++BRpt3nuzOtWR67jVGLJvV6EXJEy1LgCHFH7zZymdZr+yc0GUscDpFiUW26yWIifwQxcG/3P5nRQvhEo5ykgalm2uoHW1TW7cN6mXJty8nLm/PKfYuvYeQ3dkP4gGAsed+rEBIavvWC14p4PGYQKBgQDgGlCWqxtkYVtiQYg2rzUAH2vL5kRFOzcAqVWiHJSqrFH83a4wodMmEIrFHLQgbwUIKZCd3sBUfXQpbhEMVgKGA1NjSOgHsx5i3JdAnZnF75PHpWTfPH7hYzB96Aw9JdoQ/URNqVvoYHEVlALZNUyyaHbvxMBZjPD79EK+XkdyOQKBgQCSTs18FhCjItmFIHuF7f80PW+VnfcapKHVJCRFNontgO90GuM0f8IRS/rRGDXTZ24efXUgHZVsOC4kmEPQUBCviKKPsBeqeH116XA9nTrF3dCh0eejos5iderJ5s37Z17U2t085v8GfbzsYpM1HWu3QLmbAsDTq0z4de+r8UXs/QKBgApnzmQgj8YKazbVuiBn5DscOmIeT/8y76vdMPe1AgYmuCvYkzT43pf+3qKmYYq7/OzTGT7rT9zccl9WF/6No9JbJy0N/pAU9g07Ft7OMoypQiBcAqIG6hFHDMJb2u2f608so1ZDzdutVrLcy4EQY35JMXSSmyfNToc0YUD7ma+pAoGAQzt7Kd5p3s2Q76AxoyXu9G6W7EYe21t0EqH9GOErGLz7THtelS1wXMpxg8yAT5lj/Rbhz9RIaeaXLbHa18szH7bGHQlqlqev35NaRLYP4AVCbloq/ANAm/Zb4lPxq+mgpme86tOrtavqOSZDR1ExwpA3uofVXG+He1WWwYip/XECgYAYPwibAnvPxHcczKiBfA0jyFJbthRlzN3tFsSd5QJN14GwJ73ZFZK9Tm8WEg2p1/LY6iyrB+uNumgxs1edgCXPcqIPdGy2r5vEPL8gG4OcsKFp2GmSMq7zOo8p+LXNcoVZyxRFJTUjvs26t2z8J6n0I2YpFVGLTNRuqPFWgydgyQ==";

    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjrEVFMOSiNJXaRNKicQuQdsREraftDA9Tua3WNZwcpeXeh8Wrt+V9JilLqSa7N7sVqwpvv8zWChgXhX/A96hEg97Oxe6GKUmzaZRNh0cZZ88vpkn5tlgL4mH/dhSr3Ip00kvM4rHq9PwuT4k7z1DpZAf1eghK8Q5BgxL88d0X07m9X96Ijd0yMkXArzD7jg+noqfbztEKoH3kPMRJC2w4ByVdweWUT2PwrlATpZZtYLmtDvUKG/sOkNAIKEMg3Rut1oKWpjyYanzDgS7Cg3awr1KPTl9rHCazk15aNYowmYtVabKwbGVToCAGK+qQ1gT3ELhkGnf3+h53fukNqRH+wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url="https://36vg823507.oicp.vip/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url="http://member.gulimall.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
